#include<bits/stdc++.h>
using namespace std;
#define MAXN 200010
#define db double
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define mdI int mid=(l+r)>>1
#define pbI push_back
#define ll long long
#define MOD 998244353
#define lowbit(x) x&(-x)
#define rep(a,b,c) for(int a=b;a<=c;a++)
#define inf 1e9
void chmi(ll &x,ll y){x=min(x,y);}
void chmx(ll &x,ll y){x=max(x,y);}
ll qread(){
    char o=0;ll f=1,x=0;
    for(;!isdigit(o);o=getchar())if(o=='-')f=-1;
    for(;isdigit(o);o=getchar())x=x*10+(o-'0');
    return f*x;
}
ll qp(ll a,ll b,ll c=MOD){ll rs=1,bs=a;while(b){if(b&1)rs=rs*bs%c;bs=bs*bs%c;b>>=1;}return rs;}
bool FLA;
ll n,A[MAXN],dp[MAXN],M[1000010],MX=0;
bool FLB;
signed main(){
    cerr<<((&FLB)-(&FLA))/1024.0/1024.0<<endl;
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ll T;cin>>T;
    while(T--){
        cin>>n;rep(i,1,n)A[i]=qread();
        //build(1,1,n+1);seta(1,1,n+1,1,0);
        ll lztg=0;
        memset(dp,-0x3f,sizeof(dp));dp[0]=0;MX=0;
        memset(M,-0x3f,sizeof(M));M[0]=0;
        rep(i,1,n){
            dp[i]=MX+lztg;
            chmx(dp[i],M[A[i+1]]+lztg+A[i+1]);
            lztg+=(A[i+1]==A[i])*A[i];
            dp[i]-=lztg;
            chmx(MX,dp[i]);
            M[A[i]]=max(M[A[i]],dp[i]);
        }
        ll ans=0;
        rep(i,0,n-1)chmx(ans,dp[i]+lztg);
        cout<<ans<<endl;
    }
}

